using UnityEngine;
using System.Collections;

public delegate void OnSelectedBoardPiece(GameObject piece);

public class BoardPiece : MonoBehaviour
{
	public OnSelectedBoardPiece OnPieceSelected;
	public ClickHandler MouseClickHandler;
	private Color _highlightedColor = new Color(255,0,0);
	private Color _selectableColor = new Color (160, 0, 0);
	private Color _normalColor = new Color(0,0,0,255);
	public Material MoveableMaterial;
	public Material AttackableMaterial;
	private Material _normalMaterial;
	private bool _isSelectable = false;
	private GameObject HighlightedObjectCopy;
	void Start ()
	{
		MouseClickHandler.OnPressed += OnClicked;

		// instantiate the tile prefab and make it invisable
		HighlightedObjectCopy = GameObject.CreatePrimitive (PrimitiveType.Quad);
		HighlightedObjectCopy.transform.position = transform.position;
		HighlightedObjectCopy.transform.rotation = transform.rotation;
		HighlightedObjectCopy.transform.Translate (Vector3.forward*2);
		HighlightedObjectCopy.transform.parent = transform;
		HighlightedObjectCopy.collider.enabled = false;
		_normalMaterial = new Material (HighlightedObjectCopy.renderer.material);
	}

	public void ToggleSelectable(bool isSelectable)
	{
		_isSelectable = isSelectable;
		HighlightedObjectCopy.renderer.material = isSelectable ? AttackableMaterial : _normalMaterial;
	}

	public void OnClicked(GameObject sender)
	{
		if (_isSelectable && OnPieceSelected != null) 
			OnPieceSelected(gameObject);
	}
	public bool Selectable{
		get {return _isSelectable;}
	}
}